<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\logic;

use app\model\Repair;
use app\model\Attachment;
class RepairLogic extends BaseLogic
{

    /**
     * 设置模型
     * @return string
     */
    protected function getModelName(): string
    {
        return Repair::class;
    }

    /**
     * @Description: 分配维修员
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-05 10:08:46
     * @version: 1.0.0
     * @return [type]
     */    
    public function allotRepair($params){
        
        $where['id'] = $params['id'];
        $data = [
            'status'         => 1,
            'mark'           => $params['mark'],
            'repair_user_id' => $params['repair_user_id'],
            'update_time'    => time()
        ];

        $res = Repair::saveRepairRecord($where,$data);

        return $res;
    } 

    /**
     * @Description: 添加报修记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-11 10:28:34
     * @version: 1.0.0
     * @return [type]
     */    
    public static function addRepairRecord($params){

        $res = Repair::create($params);

        return $res;
    }

    /**
     * @Description: 获取用户全部报修记录
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $params
     * @Date: 2022-11-11 10:45:21
     * @version: 1.0.0
     * @return [type]
     */    
    public static function getRepairReocrd($params){

        if(isset($params['repair_user_id'])){
            $where['repair_user_id'] = $params['repair_user_id'];
        }else{
            $where['user_id'] = $params['user_id'];
        }
        

        $res = Repair::where($where)->order('id','desc')->limit(20)->select();

        return $res;

    }

    /**
     * @Description: 获取报修详情
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $params
     * @Date: 2022-11-19 16:42:30
     * @version: 1.0.0
     * @return [type]
     */    
    public static function getRepairDetail($params){

        $where['id'] = $params['id'];

        $res = Repair::where($where)->find();

        if($res){
          
                $res['fault_list'] = self::getImgList($res['fault_img']);

                $res['complete_list'] = self::getImgList($res['complete_img']);
            
        }
    
        return $res;
    }
    /**
     * @Description: 获取故障图片组
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $value   图片组id
     * @Date: 2022-11-05 11:31:30
     * @version: 1.0.0
     * @return [type]
     */
    protected static function getImgList($value)
    {
        $attachment = new Attachment;
        // $res = [];
        $where[] = ['id', 'in', $value];
        // echo gettype($attachment->getImg($where));
        // die;
        $res = json_decode(json_encode($attachment->getImg($where)), true);
        $arr = [];
        if($res){
            foreach ($res as $key => $value) {
                array_push($arr,$value['link']);
            }
        }
        return $arr;
    }

    /**
     * @Description: 修改报修状态
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-21 10:34:57
     * @version: 1.0.0
     * @return [type]
     */    
    public static function saveRepair($params){

        $where['id']    = $params['id'];
        // halt($params);
        unset($params['id']);
        // halt($params);
        // $saveData = [
        //     'status'        => $params['status'],
        //     'complete_img'  => $params['complete_img'],
        // ];
        $res = Repair::where($where)->update($params);

        return $res;
    }
}
